package tech.xs.common.framework.config.feign;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import tech.xs.common.framework.domain.constant.HttpHeaderKey;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        if (attributes == null) {
            return;
        }
        HttpServletRequest request = attributes.getRequest();
        if (request == null) {
            return;
        }
        template.header(HttpHeaderKey.ACCESS_TOKEN, request.getHeader(HttpHeaderKey.ACCESS_TOKEN));
        template.header(HttpHeaderKey.REQUEST_ID, request.getHeader(HttpHeaderKey.REQUEST_ID));
    }
}
